package com.cskaoyan.catmall.config;

import com.cskaoyan.catmall.realm.AdminRealm;
import com.cskaoyan.catmall.realm.WxuserRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author stone
 * @date 2022/01/10 10:10
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();

        shiroFilter.setSecurityManager(securityManager);

        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/admin/auth/login", "anon");
        filterChainDefinitionMap.put("/admin/redirect", "anon");
        filterChainDefinitionMap.put("/admin/**", "authc");



        filterChainDefinitionMap.put("wx/user/login","anon");
        filterChainDefinitionMap.put("wx/user/index","authc");
        filterChainDefinitionMap.put("wx/footprint/**","authc");
        filterChainDefinitionMap.put("wx/feedback/**","authc");
        filterChainDefinitionMap.put("wx/order/**","authc");
        filterChainDefinitionMap.put("wx/comment/post","authc");
        filterChainDefinitionMap.put("wx/address/**","authc");
        filterChainDefinitionMap.put("wx/collect/**","authc");
        filterChainDefinitionMap.put("wx/**","anon");

        shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilter;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(CustomizedModularRealmAuthenticator customizedModularRealmAuthenticator,
                                                     AdminRealm adminRealm,
                                                     WxuserRealm userRealm,
                                                     ShiroCustomSessionManager customSessionManager) {


        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        securityManager.setAuthenticator(customizedModularRealmAuthenticator);

        List<Realm> realms = new ArrayList<>();

        adminRealm.setName("admin");

        realms.add(adminRealm);
        realms.add(userRealm);
        securityManager.setRealms(realms);

//        securityManager.setRealm(customRealm);
        securityManager.setSessionManager(customSessionManager);


        return securityManager;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }




}
